// 引入pinia方法
import { defineStore } from "pinia"
// 引入 ref
import { ref } from "vue"
// 定义一个store	

interface UserState {
	tenantId : string;
	clientId : string | null;
	userType : string;
}
interface TokenData {
	token : string
	expireTime : number // 存储过期时间戳（毫秒）
}

interface UserIdentity {
	faceSide : string
	backSide : string;
	faceIsUpload : boolean;
	backIsUpload : boolean;
	name : string;
	sex : string;
	ethnicity : string;
	idNumber : string;
	address : string;
	issueAuthority : string;
	startDate : string;
	endDate : string;
	validPeriod : string;
	birthDate : string;
	maritalStatus : string;
	maritalValue : string;
	educate : string;
	educateValue : string;
	province : string;
	city : string;
	area : string;
	residentialAddress : string;
	fullAddress : string;
	companyName : string;
	workIncome : string;
	workIndustry : string;
	workIndustryValue : string;
	relationships : Array<{
		fullName : string;
		mobile : string;
		relation : string;
	}>
}



export const useInformation = defineStore('user', () => {
	// const Encryption_KEY = ref<boolean>(true)
	const access_token = ref<TokenData>({
		token: uni.getStorageSync('access_token')?.token || '',
		expireTime: uni.getStorageSync('access_token')?.expireTime || 0
	})
	const clientId = ref<string>("15f66758dac4c41d27d5eaaf97d7e188")
	const user = ref<UserState>({
		tenantId: "000000",
		clientId: uni.getStorageSync("client_id") || clientId.value,
		userType: "app_user",
	})
	const currentStep = ref<number>(uni.getStorageSync("current_step") || 0)
	const identity = ref<UserIdentity>(uni.getStorageSync("identity") || '{}')
	const phoneNumber = ref<string>(uni.getStorageSync("phoneNumber") || "")

	const setToken = (token : string, expireIn : number) => {
		const expireTime = Date.now() + expireIn * 1000
		access_token.value = {
			token,
			expireTime
		}
		uni.setStorageSync('access_token', access_token.value)
	}

	const setPhoneNumber = (newPhoneNumber : string) => {
		phoneNumber.value = newPhoneNumber
		uni.setStorageSync('phoneNumber', newPhoneNumber)
	}


	const setClientId = (newClientId : string) => {
		user.value.clientId = newClientId
		uni.setStorageSync('client_id', newClientId)
	}
	const clearUser = () => {
		access_token.value = { token: '', expireTime: 0 }
		user.value = {
			tenantId: "000000",
			clientId: clientId.value,
			userType: "app_user"
		}
		uni.removeStorageSync('access_token')
		uni.redirectTo({ url: '/pages/index/index' })
	}
	const isExpired = () => {
		return Date.now() >= access_token.value.expireTime
	}

	const setStep = (newStep : number) => {
		currentStep.value = newStep
		uni.setStorageSync('current_step', newStep)
	}

	const getIdentity = () : UserIdentity => {
		return uni.getStorageSync('identity');
	};

	const setIdentity = (newIdentity : UserIdentity) => {
		const current = getIdentity();
		const updated = { ...current, ...newIdentity };
		uni.setStorageSync('identity', updated);
		identity.value = updated;
	};

	const removeIdentity = () => {
		uni.removeStorageSync('identity')
		uni.removeStorageSync('current_step')
	};

	// 返回数据方法
	return {
		access_token,
		user,
		currentStep,
		identity,
		phoneNumber,
		setToken,
		setPhoneNumber,
		setClientId,
		clearUser,
		isExpired,
		setStep,
		getIdentity,
		setIdentity,
		removeIdentity
	}
}
	// , {
	// 	persist: {
	// 		storage: {
	// 			getItem: uni.getStorageSync,
	// 			setItem: uni.setStorageSync
	// 		}
	// 	}
	// }
)